package com.soma.game.service.impl;

import com.google.common.collect.Maps;
import com.soma.game.entity.*;
import com.soma.game.enums.BooleanStatus;
import com.soma.game.enums.GameScoreType;
import com.soma.game.mapper.GameScoreExtMapper;
import com.soma.game.mapper.GameScoreMapper;
import com.soma.game.model.RankUserVo;
import com.soma.game.service.GameScoreService;
import com.soma.game.service.GameService;
import com.soma.game.service.GameStaticService;
import com.soma.game.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class GameScoreServiceImpl extends BaseServiceImpl<GameScoreMapper, GameScore, GameScoreExample> implements GameScoreService {

    @Resource
    private UserService userService;

    @Resource
    private GameService gameService;

    @Resource
    private GameStaticService gameStaticService;

    @Resource
    private GameScoreExtMapper gameScoreExtMapper;


    @Override
    @Transactional
    public Map<String, Boolean> saveResult(Long uid, Long gameId, Long score) {

        Map<String, Boolean> result = Maps.newHashMap();

        Game game = gameService.load(gameId);

        User user = userService.getByUid(uid);

        GameScore record = getRecord(uid, gameId);
        GameScore gameScore = new GameScore();
        //更新最高得分记录
        if(record != null && record.getScore() < score){
            record.setIsRecord(BooleanStatus.N);
            save(record);
            result.put("record", true);
        }

        if(record == null || record.getScore() < score) {
            gameScore.setIsRecord(BooleanStatus.Y);
            result.put("record", true);
        }

        //保存
        gameScore.setGameId(gameId);
        gameScore.setUserId(user.getId());
        gameScore.setUid(uid);
        gameScore.setType(GameScoreType.point);
        gameScore.setScore(score);
        gameScore.setCreatedDt(new Date());
        insertSelective(gameScore);

        //更新阶段纪录
        GameStatic gameStatic = gameStaticService.getGameStatic(user, game);
        if(gameStatic.getRecordScore() < score){
            gameStatic.setRecordScore(score);
            gameStaticService.save(gameStatic);
            result.put("period", true);
        }
        return result;
    }

    @Override
    public GameScore getRecord(Long uid, Long gameId) {
        GameScoreExample example = new GameScoreExample();
        example.createCriteria().andUidEqualTo(uid).andGameIdEqualTo(gameId).andActiveFlgEqualTo(BooleanStatus.Y).andIsRecordEqualTo(BooleanStatus.Y);

        List<GameScore> gameScores = selectByExample(example);
        return gameScores.stream().findFirst().orElse(null);
    }

    @Override
    public void clearRecord(Long uid, Long gameId) {
        GameScore gameScore = getRecord(uid, gameId);
        gameScore.setScore(0l);
        save(gameScore);
    }

    @Override
    public List<RankUserVo> getRankUserVos(Long gameId, List<Long> uidList, Long uid) {
        String uids = StringUtils.join(uidList, "','");
        return gameScoreExtMapper.getRankUserVos(gameId, "'" + uids + "'", uid);
    }

    @Override
    public List<Long> getYesterdayUsers() {
        return gameScoreExtMapper.getYesterdayUsers();
    }
}